/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dataBasePlaylisted;

import controlPlaylisted.ControlMusicCommand;
import Response.ResponseMusic;
import Request.RequestMusic;
import dataPlaylisted.Music;


import java.beans.XMLDecoder;

import java.io.FileInputStream;
import java.io.ObjectInputStream;

import java.io.IOException;


import java.util.ArrayList;

/**
 *
 * @author Administrador
 */
public class getMusicListCommand implements ControlMusicCommand{
    
    public void execute(RequestMusic _req, ResponseMusic _resp){

        String file_name = "MusicModel.xml";
        FileInputStream m_fis = null;
        ObjectInputStream m_in = null;
        XMLDecoder m_xdec;

        try{
            m_fis = new FileInputStream(file_name);
        }catch(IOException e){
        }

//        try{
//            m_in = new ObjectInputStream(m_fis);
//        }catch(IOException e){
//        }

        m_xdec = new XMLDecoder(m_fis);

        ArrayList<Music> list = null;

        list = new ArrayList<Music>();

        ArrayList<String> str_list;
        str_list = new ArrayList<String>();

        try {
            str_list = (ArrayList<String>) m_xdec.readObject();
        } catch (Exception e){
        }

        if (str_list == null)
            list = new ArrayList<Music>();
        else{
            int size = str_list.size();
            int i = 0;
            while (i < size){
                Music _music;
                _music = new Music();
                _music.setTrackName(str_list.get(i));
                i++;
                _music.setArtistName(str_list.get(i));
                i++;
                _music.setStyle(str_list.get(i));
                i++;
                _music.setCammelot(str_list.get(i));
                i++;
                list.add(_music);
            }
        }
        _resp.setObjectList(list);
    }
}
